package com.acaslless.core.handler;

import com.acaslless.core.domain.R;
import com.acaslless.core.exception.FeignException;
import com.acaslless.core.exception.ServiceException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.servlet.http.HttpServletResponse;

/**
 * 全局异常处理
 *
 * @author haohan
 * @date 2025/4/10 15:27
 **/
@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {

    /**
     * 服务异常处理
     *
     * @param e 异常信息
     * */
    @ExceptionHandler(ServiceException.class)
    public R<?> serviceExceptionHandler(HttpServletResponse response, ServiceException e) {
        response.setStatus(e.getCode());
        return R.error(e.getCode(), e.getMsg());
    }

    /**
     * feign异常处理
     *
     * @param e 异常信息
     * */
    @ExceptionHandler(FeignException.class)
    public R<?> feignExceptionHandler(HttpServletResponse response, FeignException e) {
        response.setStatus(e.getCode());
        return R.error(e.getCode(), e.getMsg());
    }
}
